#include "LedLocation.h"
#include <QPaintEvent>
#include <QPainter>
#include <QApplication>
#include <QDesktopWidget>
#include <QTime>

LedLocation::LedLocation(QWidget *parent) :
	QWidget(parent)
{
	ScreenWidth = QApplication::desktop()->width();
	ScreenHeight = QApplication::desktop()->height();
	ScreenRatio = (float)ScreenWidth/ScreenHeight;
	click = QPoint(100,100);
	layout.generateLedsTest(&list);
	qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
}

void LedLocation::paintEvent(QPaintEvent *event){
	QPainter painter;
	painter.begin(this);
	painter.drawRect(start.x(),start.y(),drawWidth,drawHeight);
	for(int i=0,k=0;i<drawRects.length();i++){
		if(drawRects.at(i).type == LedEnable){
			painter.drawRect(drawRects.at(i).rect);
			painter.drawText(drawRects.at(i).rect, Qt::AlignCenter ,QString("%1").arg(k+++1));
		}
		if(drawRects.at(i).type == LedDisable){
			painter.fillRect(drawRects.at(i).rect,QColor::fromRgb(100,100,100));
		}
	}
	painter.drawEllipse(click,10,10);
	painter.end();
}

void LedLocation::resizeEvent(QResizeEvent *event){
	drawWidth = this->width();
	drawHeight = this->height();
	if(drawWidth > ScreenRatio*drawHeight) drawWidth = ScreenRatio*drawHeight;
	else drawHeight = drawWidth/ScreenRatio;
	start = QPoint((this->width()-drawWidth)/2,(this->height()-drawHeight)/2);

	updateView();
}

void LedLocation::updateView(){
	drawRects.clear();
	QList<int>* dis = layout.getDisabled();
	for(int i=0;i<dis->length();i++){
		LedView ld;
		QRect rc = layout.countRect(dis->at(i));
		ld.rect = QRect(start.x()+rc.x()*drawWidth/ScreenWidth,start.y()+rc.y()*drawHeight/ScreenHeight,
								  rc.width()*drawWidth/ScreenWidth,rc.height()*drawHeight/ScreenHeight);
		ld.type = LedDisable;
		drawRects.push_back(ld);
	}

	for(int i=0;i<list.length();i++){
		LedView ld;
		QRect rc = list.at(i)->getRect();
		ld.rect = QRect(start.x()+rc.x()*drawWidth/ScreenWidth,start.y()+rc.y()*drawHeight/ScreenHeight,
								  rc.width()*drawWidth/ScreenWidth,rc.height()*drawHeight/ScreenHeight);
		ld.type = LedEnable;
		drawRects.push_back(ld);
	}
}

void LedLocation::mouseReleaseEvent ( QMouseEvent *event){
	click = event->pos();
	if(event->button() == Qt::LeftButton){
		int type = LedEnable;
		for(int i=0;i<drawRects.length();i++){
			if(drawRects.at(i).rect.contains(click)){
				type = drawRects.at(i).type;
				break;
			}
		}
		click.setX((click.x()-start.x())*ScreenWidth/drawWidth);
		click.setY((click.y()-start.y())*ScreenHeight/drawHeight);
		layout.enableLed(click,type==LedEnable ? false : true);
		layout.generateLeds(&list);
		updateView();
		this->repaint();
	}
	if(event->button() == Qt::RightButton){
		click.setX((click.x()-start.x())*ScreenWidth/drawWidth);
		click.setY((click.y()-start.y())*ScreenHeight/drawHeight);
		layout.setStart(layout.pointToIndex(click));
		layout.generateLeds(&list);
		updateView();
		this->repaint();
	}
	if(event->button() == Qt::MiddleButton){
		layout.setSide(qrand()%2);
		layout.generateLeds(&list);
		updateView();
		this->repaint();
	}
}
